//
//  TJ.cpp
//  Chart
//
//  Created by CaoJun on 14-9-2.
//
//

#include "TJ.h"

using namespace ytx;
using namespace std;


TJ::TJ(IndexConfig indexConfig):IndexData(indexConfig, 0)
{
    indexName = "TJ";
    type = QuotationType::TJ;
}

TJ::~TJ()
{

}

void TJ::updateLastest(Quotation * data)
{
    int len = data->getDataLength();

    int n = para.value[0];

    n = (int) Tools::between(para.range[0].first, para.range[0].second, n);

    double param1a =  para.ratio[0];
    double param1b =  para.ratio[1];

    double param2a =  para.ratio[2];
    double param2b =  para.ratio[3];

    if (len < n){
        return;
    }

    vector<int> nList;

    nList.push_back(n);

    vector<double> MD = computeMA(data, nList, &KLineItem::close, len - n)[0];

    double md = MD.back();

    lineData[0].back() = md * (1+param1a/param2a);
    lineData[1].back() = md * (1+param1b/param2b);
    lineData[2].back() = md;
    lineData[3].back() = md * (1-param1b/param2b);
    lineData[4].back() = md * (1-param1a/param2a);

}

void TJ::calculate(Quotation * data)
{
    this->lineData.clear();

    int len = data->getDataLength();

    int n = para.value[0];

    double param1a =  para.ratio[0];
    double param1b =  para.ratio[1];

    double param2a =  para.ratio[2];
    double param2b =  para.ratio[3];

    n = (int) Tools::between(para.range[0].first, para.range[0].second, n);

    drawType = IndexDrawType::KIndex;

    vector<int> nList;

    nList.push_back(n);

    if (len < n){
        lineData.push_back(validation(len, len));
        lineData.push_back(validation(len, len));
        lineData.push_back(validation(len, len));
        lineData.push_back(validation(len, len));
        lineData.push_back(validation(len, len));
        return;
    }

    std::vector<double> MD = computeMA(data, nList, &KLineItem::close)[0];

    std::vector<double> UP2 = validation(n, len);

    std::vector<double> UP1 = validation(n, len);

    std::vector<double> DN1 = validation(n, len);

    std::vector<double> DN2 = validation(n, len);

    int i = n - 1;

    double md;

    while (i < len) {
        md = MD[i];
        UP2[i] = md * (1+param1a/param2a);
        UP1[i] = md * (1+param1b/param2b);
        DN1[i] = md * (1-param1b/param2b);
        DN2[i] = md * (1-param1a/param2a);
        i++;
    }

    lineData.push_back(UP2);
    lineData.push_back(UP1);
    lineData.push_back(MD);
    lineData.push_back(DN1);
    lineData.push_back(DN2);

}


vector<vector<double> > TJ::getOriginLineData()
{
    return this->originLineData;
}

void TJ::setOriginLineData(vector<vector<double> > originLineData)
{
    this->originLineData = originLineData;
}

int TJ::getDataLength() const
{
    return (int)(lineData.size()) ? (int)(lineData[0].size()) : 0;
}
